var task_id = GetQueryString('task_id');
var type = GetQueryString('type');
var modifyData;
var qxlist;
var dhbm = dhbm_sjbm;
var processDefKey = "minbobianmu";
var readflag = true;
$(document).ready(function () {
    getUserInfo();
    qxlist = UserInfo.qxlist;

	var result = ajaxResult("get",false,"/cnmuseum/bm/getdetail",{"task_id":task_id});
    if (result.status == 'OK') {
        modifyData = result.data;
        loadAddHtml();
    }

    var html = ''
    if (type == 'edit') {
        html = html + '<a type="button" class="btn btn-primary" name="btn" data-loading-text="保存"  id="saveBtn" onclick="javascript:save(this)" >保存</a> ' +
            '<a type="button" class="btn btn-primary"  name="btn" data-loading-text="提交" onclick="selectZj()" >提交</a>' +
            ' <a type="button" class="btn btn-default" onclick="javascript:window.history.back(-1)" >返回</a>'
    } else {
        html = html + '<a type="button" class="btn btn-primary"  name="btn" data-loading-text="提交审核" onclick="javascript:submit(\'tjsh\',this)" id="tjsh">提交审核</a>&nbsp;' +
            '<a type="button" class="btn btn-primary"  name="btn" data-loading-text="分配专家录入" onclick="javascript:selectZj()" id="zjlr">分配专家录入</a>' +
            '  <a type="button" class="btn btn-default" onclick="javascript:window.history.back(-1)" >返回</a>'
    }
    $('#btnDiv').html(html);
});
function selectZj() {
    var expertColumns = [
        {
            title: '选择',
            formatter: function (value, row, index) {
                return '<input name="expertId" type="checkbox" value="'
                    + row.zdbh + '" />';
            }
        }, {
            title: '专家名称',
            field: 'xm',
            width: 50
        }, {
            title: '所在机构',
            field: 'szjg'
        }, {
            title: '研究特长',
            field: 'yjtc'
        }];
    createTable('#userInfo', '../../mbXtglZjkServlet.getZjkUser.do', 'zdbh', expertColumns, null, null);
    $("#nextExpertModal").modal('show');
}

// 提交
function submit(nextstep,ele) {
	$("a[name=btn]").button('loading').delay(1000).queue(function() {
		$("a[name=btn]").button('reset');
		$("a[name=btn]").dequeue(); 
	});
	$(ele).button('loading').delay(1000).queue(function() {
		$(ele).button('reset');
		$(ele).dequeue(); 
		if (nextstep == '') {
			alertFunc('下一步骤不能为空');
			return;
		}
		// 验证
		if (valid(nextstep)) {
			// 获取参数
			var params = getParams(nextstep);
			// 提交
			var result = ajaxResult('POST', false,'/cnmuseum/bm/submit',params);
			if (result.status == 'OK') {
				alertFunc('提交成功', function () {
					location.href = 'sjbm.html';
				});
			}
		}
	});
}
function getParams(nextstep) {
	var params = {};
	var params=$('#addForm').serializeObject();
	if (nextstep == 'zjlr') {
		var assigneeuser = new Array;// =$("input[name='expertId']:checked").val();
		$.each($("input[name='expertId']:checked"), function(index, data) {
			assigneeuser.push(data.value);
		});
		params.nextids=assigneeuser.toString();
		params.next='lr';
	} else if (nextstep == 'tjsh') {
		params.next = 'sh';
	}
	return params;
}
function valid(nextstep) {
    if (nextstep == 'zjlr') {
        var assigneeuser = new Array;// =$("input[name='expertId']:checked").val();
        $.each($("input[name='expertId']:checked"), function (index, data) {
            assigneeuser.push(data.value);
        });
        if (!checkExpert(assigneeuser, dhbm_bjlr)) {
            return true;
        } else {
            alertFunc('所选择的专家有权限重复，请重新选择！');
        }
        return false;

    } else
        return true;
}

//数据录入的保存
function save(ele) {
	$("a[name=btn]").button('loading').delay(1000).queue(function() {
		$("a[name=btn]").button('reset');
		$("a[name=btn]").dequeue(); 
	});
	$(ele).button('loading').delay(1000).queue(function() {
		$(ele).button('reset');
		$(ele).dequeue(); 
		var params = $('#addForm').serializeObject();
		var result = ajaxResult('POST',false,'/cnmuseum/bm/save',params);
		if (result.status == 'OK') {
			alertFunc('保存成功', function () {
				window.location.href = 'sjbm.html';
			});
		}
	});
}
